import { connect } from 'umi';
import { ISupplierActionEnum, SupplierLocaldataContext, useSupplierReducer } from '../common';
import { useEffect } from 'react';
import { Col, Row, Spin, message } from 'antd';
import { UpdateViewRight } from './UpdateViewRight';
import { DetailVliewLeft } from './DetailViewLeft';
import { getSupplierByIdService } from '../services';
// import MockData from '../common/MockManage.json';

const memoirImageRenderID = `supplierDetailBoxRow${Math.random()}`;

const SupplierDetailView: React.FC<SupplierManageAPI.ISupplierDetailView> = ({ curRes, locationProps }) => {
  const updateFixCls = 'supplier-update-'; //表单样式根目录

  /** 获取reducer数据 */
  const { supplierDispath, contactsSwitchList, contactsCurrent, qualifyDataList, memoirDataList } = useSupplierReducer();

  /**  初始化获取详情接口 */
  useEffect(() => {
    if (locationProps?.state?.id) {
      reloadDetailInfo();
    }

    // supplierDispath({
    //   //模拟数据-王志龙
    //   type: ISupplierActionEnum.CHECKLIST_CONTACTS,
    //   payload: MockData?.[0]?.msupplierContacters,
    // });
  }, [locationProps?.state]);

  /**
   * 获取详情数据
   * @param res
   */
  const getSupplierByIdCallback = (res: any) => {
    if (res && res?.msgFlag) {
      /** 供应商联系人 */
      supplierDispath({
        type: ISupplierActionEnum.CHECKLIST_CONTACTS,
        payload: res?.obj?.msupplierContacters || [],
      });
    } else {
      message.error(res?.msg);
    }
  };

  /** 引入详情接口 */
  const { getSupplierByIdLoading, getSupplierByIdRun, getSupplierByIdInfo } = getSupplierByIdService(getSupplierByIdCallback);

  /**  刷新数据详情页面 */
  const reloadDetailInfo = () => {
    const payload: Record<string, any> = {
      id: locationProps?.state?.id,
    };
    getSupplierByIdRun(payload);
  };
  return (
    <SupplierLocaldataContext.Provider
      value={{
        updateFixCls,
        curRes,
        locationID: locationProps?.state?.id,
        supplierDispath,
        contactsSwitchList,
        supplierInfoObj: getSupplierByIdInfo,
        // supplierInfoObj: MockData?.[0], //模拟数据-王志龙
        memoirImageRenderID,
        reloadDetailInfo,
        contactsCurrent,
        qualifyDataList,
        memoirDataList,
      }}
    >
      <Spin spinning={getSupplierByIdLoading}>
        <Row id={memoirImageRenderID} gutter={[8, 8]} className={updateFixCls + 'row'}>
          <Col span={24} className={updateFixCls + 'row-col'}>
            <DetailVliewLeft />
          </Col>
          {/* <Col span={8} className={updateFixCls + 'row-col'}>
            <UpdateViewRight />
          </Col> */}
        </Row>
      </Spin>
    </SupplierLocaldataContext.Provider>
  );
};
export default connect(({ baseLogin }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
}))(SupplierDetailView);
